{% extends "layout.html" %}
{% block __data_browser__custom_external_block %}
  {{ super() }}
  <script type="text/javascript">
    $(function () {
      $('#myTab a:first').tab("show");
    });
  </script>
{% endblock %}
{% block body %}
  <div class="container">
    {% if not keywords %}
      <form class="form-horizontal" action="{{ url_for('search.search') }}" method="GET">
        <div class="form-group">
          <label for="content-input" class="control-label col-lg-2">请输入关键字</label>

          <div class="col-lg-3">
            <input id="content-input" type="search" placeholder="搜索..." name="content" class="form-control"/>
             <span class="help-block">{% if permissions.roles.CargoClerkPermission.can() %}
               只支持搜索卸货会话（发货会话）的车牌号码或客户名称，以及订单的订单编号
             {% elif permissions.roles.SchedulerPermission.can() %}
               只支持搜索订单的订单编号或者工单号
             {% endif %}
             </span>
          </div>
        </div>
        <div class="form-group">
          <div class="col-lg-offset-2 col-lg-3">
            <button class="btn btn-primary btn-lg"><i class="fa fa-search fa-fw"></i>搜索</button>
          </div>
        </div>
      </form>
    {% else %}
      <strong><em>{{ keywords }}</em></strong> 的搜索结果:
      <ul class="nav nav-tabs nav-justified" id="myTab">
        {% if permissions.roles.CargoClerkPermission.can() %}
          <li>
            <a href="#unload_session_list" data-toggle="tab">卸货会话({{ unload_session_list|count }})</a>
          </li>
          <li>
            <a href="#delivery_session_list" data-toggle="tab">发货会话({{ delivery_session_list|count }})</a>
          </li>
        {% endif %}
        {% if permissions.order.view_order.can() or permissions.order.schedule_order.can() %}
          <li>
            <a href="#order_list" data-toggle="tab">订单({{ order_list|count }})</a>
          </li>
        {% endif %}
        {% if permissions.roles.SchedulerPermission.can() %}
          <li>
            <a href="#work_command_list" data-toggle="tab">工单({{ work_command_list|count }})</a>
          </li>
        {% endif %}
      </ul>
      <div class="tab-content">
        {% if permissions.roles.CargoClerkPermission.can() %}
          <div id="unload_session_list" class="tab-pane">
            {% if not unload_session_list %}
              <p class="text-danger">
                对不起，没有客户名称或者车牌号为<em>{{ keywords }}</em>的卸货会话!
              </p>
            {% else %}
              {% for us in unload_session_list %}
                <div class="panel panel-default">
                  <div class="panel-body">
                    {{ loop.index }}.
                    <a href="{{ url_for("cargo.unload_session",id_=us.id, url=request.url) }}">
                      卸货会话: {{ us.id }}
                    </a>
                    -车牌号: <em>{{ us.plate }}</em>
                    -客户名称: <em>{{ us.task_list|groupby("customer.name")|join("，",attribute="grouper") }}</em>
                    <blockquote>--在{{ us.create_time }}创建</blockquote>
                  </div>
                </div>
              {% endfor %}
            {% endif %}
          </div>
          <div id="delivery_session_list" class="tab-pane">
            {% if not delivery_session_list %}
              <p class="text-danger">
                对不起，没有客户名称或者车牌号为<em>{{ keywords }}</em>的发货会话!
              </p>
            {% else %}
              {% for ds in delivery_session_list %}
                <div class="panel panel-default">
                  <div class="panel-body">
                    {{ loop.index }}.
                    <a href="{{ url_for("delivery.delivery_session",id_=ds.id, url=request.url) }}">
                      发货会话: {{ ds.id }}
                    </a>
                    -车牌号: <em>{{ ds.plate }}</em>
                    -客户名称: <em>{{ ds.delivery_task_list|join(",", attribute="customer.name") }}</em>
                    <blockquote>--在{{ ds.create_time }}创建</blockquote>
                  </div>
                </div>
              {% endfor %}
            {% endif %}
          </div>
        {% endif %}
        {% if permissions.order.view_order.can() or permissions.order.schedule_order.can() %}
          <div id="order_list" class="tab-pane">
            {% if not order_list %}
              <p class="text-danger">
                对不起，没有编号为<em>{{ keywords }}</em>的订单!
              </p>
            {% else %}
              {% for order in order_list %}
                <div class="panel panel-default">
                  <div class="panel-body">
                    {{ loop.index }}.
                    {% if permissions.order.view_order.can() or permissions.order.schedule_order.can() %}
                      <a href="{{ url_for('order.order',id_=order.id, url=request.url) }}">
                    {% endif %}
                    订单: {{ order.customer_order_number }}
                    {% if permissions.order.view_order.can() or permissions.order.schedule_order.can() %}
                      </a>
                    {% endif %}
                    - 客户: <em>{{ order.customer.name }}</em>
                    - 产品: <em>{{ order.product_list|join(",", attribute="name") }}</em>
                    - 重量: <em>{{ order.sub_orders|sum(attribute="weight") }}(公斤)</em>
                    - {% if order.dispatched %}<em>已经下发</em>{% else %}<em>尚未下发</em>{% endif %}
                    <blockquote>--由{{ order.creator.username }}在{{ order.create_time }}创建
                    </blockquote>
                  </div>
                </div>
              {% endfor %}
            {% endif %}
          </div>
        {% endif %}

        {% if permissions.roles.SchedulerPermission.can() %}
          <div id="work_command_list" class="tab-pane">
            {% if not work_command_list %}
              <p class="text-danger">
                对不起，没有编号为<em>{{ keywords }}</em>的工单!
              </p>
            {% else %}
              {% for wc in work_command_list %}
                {{ loop.index }}.
                <a href="{{ url_for("manufacture.work_command",id_=wc.id, url=request.url) }}">
                  {{ wc.id }}
                </a>
                -客户: <em>{{ wc.sub_order.order.goods_receipt.customer.name }}</em>
                -产品: <em>{{ wc.sub_order.product.name }}</em>
                -车间: <em>{{ wc.department.name or "未分配" }}</em>
                -班组: <em>{{ wc.team.name or "未分配" }}</em>
                -状态: <em>{{ wc.status_name }}</em>
                <blockquote>--在{{ wc.create_time }}创建</blockquote>
              {% endfor %}
            {% endif %}
          </div>
        {% endif %}
      </div>
    {% endif %}
  </div>
{% endblock %}
